package com.zyms.core.filter;

import com.zyms.core.util.ResposeUtil;
import com.zyms.module.result.ResultObj;
import com.zyms.module.result.ReturnCode;
import com.zyms.myRunTime.MyRunTimeExcption;
import com.zyms.service.TokenService;
import com.zyms.tools.json.JsonHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.context.support.XmlWebApplicationContext;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by Baggio on 2018-2-2.
 */
public class TokenFilter extends OncePerRequestFilter {

    @Autowired
    TokenService tokenService;

    @Override
    protected void doFilterInternal(HttpServletRequest request,
                                    HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        // 不过滤的uri
        String[] notFilter = new String[]{"/static",
                "/createToken"};

        // 请求的uri
        String uri = request.getRequestURI();
        // 是否过滤
        boolean doFilter = true;

        for (String s : notFilter) {
            if (uri.indexOf(s) != -1) {
                // 如果uri中包含不过滤的uri，则不进行过滤
                doFilter = false;
                break;
            }
        }

        if (doFilter) {


            HttpServletRequest req = (HttpServletRequest)request;
            ServletContext sc = req.getSession().getServletContext();
            XmlWebApplicationContext cxt = (XmlWebApplicationContext) WebApplicationContextUtils.getWebApplicationContext(sc);

            if(cxt != null && cxt.getBean("tokenService") != null && tokenService == null){
                tokenService = (TokenService) cxt.getBean("tokenService");
            }

            System.out.println("====================true===================================>>>>>>>>>>>>>>");
            ResultObj resultObj = new ResultObj(ReturnCode.SYSTEM_ERRO);
            String token = request.getParameter("token");
            try {
                if (tokenService.verifyToken(token)) {
                    System.out.println("============tongguo===========================================>>>>>>>>>>>>>>");
                    filterChain.doFilter(request, response);
                    return;
                }
            } catch (MyRunTimeExcption myRunTimeExcption) {
                myRunTimeExcption.printStackTrace();
                resultObj.setCode(myRunTimeExcption.getErroCode());
                resultObj.setMsg(myRunTimeExcption.getMessage());
            } catch (IOException e) {
                e.printStackTrace();
            } catch (ServletException e) {
                e.printStackTrace();
            }
            ResposeUtil.writeContent(JsonHandler.objectToJson(resultObj), response);
            return;
        }

        // 如果不执行过滤，则继续
        filterChain.doFilter(request, response);
    }
}
